<?php

/**
 * 课程管理
 */
namespace app\avikcm\controller;
use think\Db;
use think\facade\Request;

class Course extends Common
{
    public function initialize()
    {
        parent::initialize();
    }

    // 列表
    public function list(){
        if(Request::isAjax()){
            $key = input('post.key');
            $type = input('post.type');
            $category = input('post.category');
            $this->assign('testkey', $key);
            $page = input('page', 1);
            $pageSize = input('limit', config('pageSize'));
            $where [] = ['a.del','=',0];
            if ($key) {
                $where[] = ['a.title', 'like', "%" . $key . "%"];
            }
            if($category){
                $where[] = ['a.category','=',$category];
            }
            if($type){
                $where[] = ['a.type','=',$type];
            }
            $paginate = ['list_rows'=>$pageSize,'page'=>$page];
            $list = db('course')->alias('a')->where($where)->order('update_time', 'desc')->paginate($paginate)->toArray();

            if($list['data']){
                $course_type = config('course_type');
                foreach ($list['data'] as $key=>$val){
                    $type_name = $course_type[$val['type']];
                    $category_name = db('course_category')->where('id',$val['category'])->value('title');
                    $list['data'][$key]['category_name'] = $category_name;
                    $list['data'][$key]['type_name'] = $type_name;
                }
            }
            return ['code'=>0,'msg'=>'成功','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        $course_category = db('course_category')->field('id,title')->where(['pid'=>0,'del'=>0])->select();
        $this->assign('course_category', $course_category);
        $this->assign('course_type', config('course_type'));
        return $this->fetch();
    }

    // 添加
    public function add()
    {
        if (Request::isAjax()) {
            //构建数组
            $data = Request::except('file');
            $data['create_time'] = date("Y-m-d H:i:s", time());
            $data['update_time'] = date("Y-m-d H:i:s", time());
            if($data['password']){
                $data['password'] = md5($data['password']);
            }
            $insert_id = Db::table('course')->insertGetId($data);
            $result['code'] = 1;
            $result['msg'] = '添加成功!';
            $result['id'] = $insert_id;
            $result['url'] = url('list');
            return $result;
        } else {
            $course_id = input('course_id');
            $brand = db('goods_brand')->where(['del' => 0])->select();
            $course_category = db('course_category')->field('id,title')->where(['pid'=>0,'del'=>0])->select();
            $itemList = db('course_item')->where('course_id', $course_id)->order('sort')->select();

            $this->assign('brand', $brand);
            $this->assign('course_category', $course_category);
            $this->assign('course_type', config('course_type'));
            $this->assign('title', '添加课程');
            $this->assign('info', 'null');
            $this->assign('itemList', $itemList);
            return $this->fetch('form');
        }
    }


    // 修改
    public function edit(){
        $tab = input('tab');
        $id = input('id');
        if(Request::isAjax()){
            //构建数组
            $data = Request::except('file');
            $data['update_time'] = date("Y-m-d H:i:s");
            if ($data['password']) {
                $data['password'] = md5($data['password']);
            }
            db('course')->update($data);
            $result['code'] = 1;
            $result['msg'] = '修改成功!';
            $result['url'] = url('list');
            return $result;
        }else{
            $info = db('course')->where('id',$id)->find();
            $brand = db('goods_brand')->where(['del' => 0])->select();
            $course_category = db('course_category')->field('id,title')->where(['pid' => 0, 'del' => 0])->select();
            $itemList = db('course_item')->where('course_id', $id)->order('sort')->select();
            $this->assign('brand', $brand);
            $this->assign('course_category', $course_category);
            $this->assign('course_type', config('course_type'));
            $this->assign('info',json_encode($info,true));
            $this->assign('baseInfo',$info);
            $this->assign('itemList', $itemList);
            $this->assign('tab', $tab); 
            $this->assign('title','编辑课程');
            return $this->fetch('form');
        }
    }
   
    // 删除
    public function del(){
        $id = input('id');
        db('course')->where('id',$id)->update(['del'=>1]);
        return ['code'=>1,'msg'=>'删除成功！'];
    }

    // 批量删除
    public function delAll(){
        $map[] =array('id','in',input('param.ids/a'));
        db('course')->where($map)->update(['del'=>1]);
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        return $result;
    }

    // 删除
    public function delItem()
    {
        $id = input('id');
        db('course_item')->where('id', $id)->delete();
        return ['code' => 1, 'msg' => '删除成功！'];
    }

    // 添加
    public function addItem()
    {
        $course_id = input('course_id');
        if (Request::isAjax()) {
            //构建数组
            $data = Request::except('file');
            $data['create_time'] = date("Y-m-d H:i:s", time());
            $data['update_time'] = date("Y-m-d H:i:s", time());
            $insert_id = Db::table('course_item')->insertGetId($data);
            $result['code'] = 1;
            $result['msg'] = '添加成功!';
            $result['id'] = $insert_id;
            $result['url'] = url('edit').'?id=' . $data['course_id'] . '&tab=two';
            return $result;
        } else {
            $this->assign('title', '添加课时');
            $this->assign('info', 'null');
            $this->assign('course_id', $course_id);
            return $this->fetch('edit_item');
        }
    }

    // 编辑
    public function editItem()
    {
        $id = input('id');
        if (Request::isAjax()) {
            //构建数组
            $data = Request::except('file');
            $data['create_time'] = date("Y-m-d H:i:s", time());
            $data['update_time'] = date("Y-m-d H:i:s", time());
            Db::table('course_item')->where('id', $id)->update($data);
            $item = db('course_item')->where('id', $id)->find();
            $result['code'] = 1;
            $result['msg'] = '编辑成功!';
            $result['url'] = url('edit') . '?id=' . $item['course_id'] . '&tab=two';
            return $result;
        } else {
            $info = Db::table('course_item')->where('id', $id)->find();
            $this->assign('title', '编辑课时');
            $this->assign('info', json_encode($info));
            return $this->fetch();
        }
    }

    public function getCategory()
    {
        $id = input('id');
        $lists = db('course_category')->where(['del' => 0, 'pid' => $id])->select();
        return $lists;
    }



}